package com.jeeplus.test.jlnu.studentlogin.service.impl;

import com.jeeplus.test.jlnu.studentlogin.mapper.StudentLoginMapper;
import com.jeeplus.test.jlnu.studentlogin.service.StudentLoginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentLoginServiceImpl implements StudentLoginService {

    @Autowired
    private StudentLoginMapper studentLoginMapper;

    @Override
    public int check(String studentId, String idCard, String level) {
        if(StringUtils.isBlank(level)){
            int check = studentLoginMapper.check(studentId, idCard);
            return check;
        }else {
            String graduation = studentLoginMapper.checkGradation(studentId, idCard);

            // 如果 gradation 为空，则返回 0
            if (StringUtils.isBlank(graduation)) {
                return 0;
            }

            // 匹配学历级别并检查数据库
            if (StringUtils.equals(graduation, level) && studentLoginMapper.check(studentId, idCard) > 0) {
                return 1;
            }

            return 0;
        }
    }

    @Override
    public int changePasswd(String studentId, String newPasswd) {
        return studentLoginMapper.changePasswd(studentId,newPasswd);
    }

    @Override
    public String checkIsChangePasswd(String studentId) {
        return studentLoginMapper.checkIsChangePasswd(studentId);
    }

    @Override
    public int useNewPasswd(String studentId, String idCard, String level) {
        //改密码时也要验证一下但是不用验证学历是什么了 所以level传空
        if(StringUtils.isBlank(level)){
            int check = studentLoginMapper.useNewPasswd(studentId, idCard);
            return check;
        }else {
            String gradation = studentLoginMapper.checkGradationNew(studentId, idCard);

            // 如果 gradation 为空，则返回 0
            if (StringUtils.isBlank(gradation)) {
                return 0;
            }

            // 匹配学历级别并检查数据库
            if (StringUtils.equals(gradation, level) && studentLoginMapper.useNewPasswd(studentId, idCard) > 0) {
                return 1;
            }

            return 0;
        }
    }
}